import axios from 'axios';
import qs from 'query-string';
import type { RouteRecordNormalized } from 'vue-router';
import { UserState } from '@/store/modules/user/types';

export interface LoginData {
  username: string;
  password: string;
}

export interface phoneData {
  phone: string;
  verificationCode: string;
}

export interface codeData {
  phone: string;
}

export interface LoginRes {
  role: number;
  token: string;
}
export function login(data: LoginData) {
  return axios.post<LoginRes>('/user/user/login', data);
}

export function phoneLogin(data: phoneData) {
  console.log(data);

  return axios.post<LoginRes>('/user/user/verificationLogin', data);
}

export function sendCode(data: codeData) {
  // console.log("创建问卷");
  // console.log(userid);
  return axios({
    url: '/user/user/sendCode',
    method: 'POST',
    data: { phone: data },
  });
  // return axios.post('/user/user/sendCode', data);
  // return axios.post('/survey/survey', params,data );
}

export function logout() {
  return axios.post<LoginRes>('/api/user/logout');
}

export function register(data) {
  console.log('register');
  return axios.post('/user/user/register', data);
}

// 密保注册的
export function registerQuestions(data1, data) {
  console.log('registerQuestions');
  console.log(data);

  return axios({
    url: '/user/secretquestion',
    method: 'POST',
    params: { username: data1 },
    data,
  });
}

export function getUserInfo() {
  return axios.post<UserState>('/api/user/info');
}

// export function createZuhu() {
//   return axios.post('/user/user/login');
// }

export function getMenuList() {
  return axios.post<RouteRecordNormalized[]>('/api/user/menu');
}

export function modifyZuhu(data) {
  console.log(data);
  return axios.put('/user/user', data);
}

// 查询用户修改的历史
export function useIdFindHistory(params) {
  console.log('查询用户历史');

  return axios.get('/user/userhistory/page', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 软删除用户
export function deleteUser(data) {
  return axios({
    url: '/user/user/logic',
    method: 'DELETE',
    data,
  });
}

// 查询群组修改的历史
export function useGroupIdFindHistory(params) {
  console.log('查询群组历史');

  return axios.get('/user/grouphistory/page', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 删除用户修改的历史
export function useIdDeleteHistory(data) {
  return axios({
    url: '/user/userhistory',
    method: 'DELETE',
    data,
  });
}

// 删除群组修改的历史
export function useGroupIdDeleteHistory(data) {
  return axios({
    url: '/user/grouphistory',
    method: 'DELETE',
    data,
  });
}

// export function sendCode(params,data) {
//   return axios({
//     url: '/user/user/sendCode',
//     method: 'POST',
//     params: params,
//     data,
//   });
//   // return axios.post(,data);
// }

// 找回密码的一堆请求

// 首先是根据用户名找问题
export function useUsernameFindSecurityQuestion(data) {
  // console.log('data.value.username');
  // console.log(data.value.username);
  // console.log('data.value.username');

  return axios.get(`/user/secretquestion`, {
    params: { username: data.value.username },
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 然后是根据密保问题和答案去验证 去改密码
export function useAnswerChangePassword(data) {
  console.log('data');
  console.log(data);
  console.log('data');
  return axios({
    url: '/user/secretquestion/secret/retrieve',
    method: 'POST',
    data,
  });
}

// 获取验证码
export function usePhoneObtainCode(data) {
  return axios({
    url: '/user/secretquestion/sendCode',
    method: 'POST',
    data,
  });
}

// 验证码重置密码
export function usePhoneChangePassword(data) {
  return axios({
    url: '/user/secretquestion/reset',
    method: 'POST',
    data,
  });
}
